/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Beans.NhanVien;
import Models.NhatKyHT_Model;
import Models.DonViTinh_Model;

/**
 *
 * @author Venus
 */
public class DVTinh_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                DonViTinh_Model nmam = new DonViTinh_Model();
                Vector vDSDVT = new Vector();
                vDSDVT = nmam.LayDSDVT(langid);
                request.setAttribute("DSDVT", vDSDVT);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newdvt.jsp");
                rd.forward(request, response);
            } else if (action.equals("Add")) {
                String dvtname = request.getParameter("dvtname");
                if (DonViTinh_Model.KiemTraTenDVT(dvtname)) {
                    out.print("exist"); // khu vực đã tồn tại
                } else {
                    int madvt = DonViTinh_Model.TaoMa();
                    DonViTinh_Model.AddDVT(madvt, dvtname);
                    NhanVien nv = new NhanVien();
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "unit");
                    DonViTinh_Model dvtm = new DonViTinh_Model();
                    Vector v = new Vector();
                    v = dvtm.Detail(madvt);
                    request.setAttribute("Detail", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/dvtlang.jsp");
                    rd.forward(request, response);
                }
            } else if (action.equals("ShowDVT")) {
                String item = request.getParameter("item");
                DonViTinh_Model dvtm = new DonViTinh_Model();
                Vector v = new Vector();
                v = dvtm.LayDSDVT(langid);
                request.setAttribute("DSDVT", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listdvt.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowLangDVT")) {
                String madvt = request.getParameter("madvt");
                DonViTinh_Model dvtm = new DonViTinh_Model();
                Vector v = new Vector();
                v = dvtm.Detail(Integer.parseInt(madvt));
                request.setAttribute("Detail", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/dvtlang.jsp");
                rd.forward(request, response);
            } else if (action.equals("Edit")) {
                String tendvt = request.getParameter("tendvt");
                String madvt = request.getParameter("madvt");
                String mann = request.getParameter("mann");
                if (DonViTinh_Model.KiemTraSuaTenDVT(tendvt, madvt)) {
                    out.print("exist");
                } else {
                    DonViTinh_Model.Edit(madvt, mann, tendvt);
                    NhanVien nv = new NhanVien();
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "unitname");
                    DonViTinh_Model dvtm = new DonViTinh_Model();
                    Vector v = new Vector();
                    v = dvtm.Detail(Integer.parseInt(madvt));
                    request.setAttribute("Detail", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/dvtlang.jsp");
                    rd.forward(request, response);
                }
            } else if (action.equals("SortTenDVT")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                DonViTinh_Model dvtm = new DonViTinh_Model();
                Vector v = new Vector();
                v = dvtm.SortTenDVT(langid, order);
                request.setAttribute("DSDVT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortdvt.jsp");
                rd.forward(request, response);
            } else if (action.equals("Search")) {
                String tendvt = request.getParameter("tendvt");
                String item = request.getParameter("item");
                DonViTinh_Model dvtm = new DonViTinh_Model();
                Vector v = new Vector();
                v = dvtm.Search(tendvt, langid);
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("item", item);
                request.setAttribute("DSDVT", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchdvt.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenDVT")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tendvt = request.getParameter("tendvt");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                DonViTinh_Model dvtm = new DonViTinh_Model();
                Vector v = new Vector();
                v = dvtm.SortSearchTenDVT(langid, order, tendvt);
                request.setAttribute("DSDVT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("tendvt", tendvt);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchdvt.jsp");
                rd.forward(request, response);
            }

        } finally {
            out.close();
        }
    }    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
